define(['require' , 'exports' , 'module' , 'jquery' , 'common/common' , 'layer' , 'dmuploader' , 'sortable'] ,
    function (require , exports , module , $ , common , layer) {

    var message = {
        addLog: function (id, status, str) {
            var d = new Date();
            var li = $('<li />', {'class': 'demo-' + status});

            var message = '[' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds() + '] ';

            message += str;

            li.html(message);

            $(id).prepend(li);
        },

        addFile: function (id, i, file) {
            var url = window.URL || window.webkitURL;
            var imgURL = URL.createObjectURL(file);
            var template = '<div style="overflow: hidden;float: left;height: 186px;margin: 15px 0;" class="col-lg-3" id="demo-file' + i + '">' +
                '<div class="remove"><i class="icon-remove"></i></div>'+
                '<img class="goods-imgs" id="img-' + i + '" style="width: 100%;height:100%;" src="'+ imgURL +'">'+
                // '<span class="demo-file-id">#' + i + '</span> - ' + file.name + ' <span class="demo-file-size">(' + message.humanizeSize(file.size) + ')</span> - Status: <span class="demo-file-status">Waiting to upload</span>' +
                '<div style="position: absolute;top: 50%;margin-left: -15px;z-index: 100;" class="col-lg-12">'+
                '<div class="progress progress-striped active">' +
                '<div class="progress-bar" role="progressbar" style="width: 0%;">' +
                '<span class="sr-only">0% Complete</span>' +
                '</div>' +
                '</div>' +
                '</div>' +
                '</div>';

            var itemnum = $(id).attr('file-counter');
            if (!itemnum) {
                $(id).empty();

                i = 0;
            }

            i++;

            $(id).attr('file-counter', i);

            $(id).append(template);
        },

        updateFileStatus: function (i, status, message) {
            $('#demo-file' + i).find('span.demo-file-status').html(message).addClass('demo-file-status-' + status);
        },

        updateFileProgress: function (i, percent) {

            $('#demo-file' + i).find('div.progress-bar').width(percent);

            $('#demo-file' + i).find('span.sr-only').html(percent + ' Complete');

        },

        humanizeSize: function (size) {
            var i = Math.floor(Math.log(size) / Math.log(1024));
            return ( size / Math.pow(1024, i) ).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
        }
    };

    var ajaxAddManufactor = function () {
        $('.form-submit').bind('click' , function () {
            var layerLoad = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            var formData = {};
            var formDataItem = $('.form-control');

            formDataItem.each(function (index , item) {
            	if(index != 0) {
            		formData[$(item).attr('name')] = $(item).val();
            	}
            });

            common.ajaxPost('/admin/manufactor/add', formData, function (error, data) {
                if (error == null && data.code == 0) {
                    layer.msg('添加成功');
                    window.location.reload();
                } else if (error == null && data.code != 0) {
                    layer.alert(data.msg);
                } else {
                    layer.alert(error.statusText);
                }
                layer.close(layerLoad);
            });
        });
    };
    
    var ajaxUpdateManufactor = function () {
        $('.form-submit').bind('click' , function () {
            var layerLoad = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            var formData = {};
            var formDataItem = $('.form-control');

            formDataItem.each(function (index , item) {
                var itemName = $(item).attr('name');
                if(itemName != undefined) {
                    if (index != 0) {
                        if ($(item).attr('name') == 'status') {
                            if ($(item).attr('checked') == 'checked') {
                                formData[itemName] = $(item).val();
                            }
                        } else {
                            formData[itemName] = $(item).val();
                        }
                    }
                }
            });

            var sort = 1;

            common.ajaxPost('/admin/manufactor/update', formData, function (error, data) {
                if (error == null && data.code == 0) {
                    layer.msg('修改成功');
                    window.location.reload();
                } else if (error == null && data.code != 0) {
                    layer.alert(data.msg);
                } else {
                    layer.alert(error.statusText);
                }
                layer.close(layerLoad);
            });
        });
    };

    var updateGoodsStatus = function () {
        $('.update-status').bind('click' , function () {
            var gid = $(this).attr('gid');
            var status = $(this).attr('status');

            var layerLoad = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });

            common.ajaxPost('/admin/goods/update/status', {id:gid , status:status}, function (error, data) {
                if (error == null && data.code == 0) {
                    layer.msg('修改成功');
                    window.location.reload();
                } else if (error == null && data.code != 0) {
                    layer.alert(data.msg);
                } else {
                    layer.alert(error.statusText);
                }
                layer.close(layerLoad);
            });
        });
    };

    var deleteNotice = function () {
        $('.delete').bind('click' , function () {
            var href = $(this).attr('href');
            var confirm = layer.confirm('您确定要删除此供应商？', {
                btn: ['确定','取消'] //按钮
            }, function(yes){
                layer.close(yes);
                var layerLoad = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                $.ajax({
                    url     : href,
                    type    : 'GET',
                    dataType : 'json' ,
                    success : function (data) {
                        if(data.code == 0){
                            window.location.reload();
                        }else{
                            layer.alert(data.msg);
                        }
                        layer.close(layerLoad);
                    },
                    error : function (error) {
                        layer.alert(error.statusText);
                        layer.close(layerLoad);
                    }
                });
            }, function(no){
                layer.close(no);
            });
        });
    };
    
    exports.updateGoodsStatus = updateGoodsStatus;

    exports.ajaxAddManufactor = ajaxAddManufactor;
    exports.ajaxUpdateManufactor = ajaxUpdateManufactor;
    exports.deleteNotice = deleteNotice;
});